<template>
  <div class="page-box">
    <!-- 搜索部分 -->
    <seachFrom @getTableData="getTableData" ref="seachFrom"></seachFrom>
    <!-- 操作部分  -->
    <!-- <div class="operation-box cenBet">
            <div class="operation-left cenLeft">
                <el-button v-if="$store.state.activityId.myPower==1||$store.state.activityId.myPower==2" @click="delTicketByOrderId()" type="danger" icon="el-icon-delete">作废</el-button>
                <el-button @click="goBackMoney()" type="warning">退款</el-button>
                <div v-if="sumPage" class="operation-tit" style=" margin-left: 16px;"> 共
                    <span>{{sumPage}}</span> 笔订单</div>
            </div>
        </div> -->
    <!-- 表格部分 -->
    <div class="table-box">
      <!-- <el-card :body-style="{ padding: '0px' }" shadow="hover"> -->
      <el-table @selection-change="handleSelectionChange" ref="multipleTable" v-loading="tableLoading" border element-loading-text="拼命加载中" @expand-change="expandChange" :data="tableData">

        <el-table-column prop="operationType" align="center" label="操作">

        </el-table-column>
        <el-table-column prop="description" align="center" label="说明">

        </el-table-column>
        <el-table-column prop="operatorName" align="center" label="操作人">

        </el-table-column>
        <el-table-column align="center" label="操作时间">
          <template slot-scope="scope">
            <div>
              {{$moment(scope.row.gmtCreate)}}
            </div>

          </template>
        </el-table-column>
      </el-table>
      <!-- </el-card> -->
    </div>
    <div v-if="sumPage" class="pagination-box">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="thisPage" :page-sizes="$pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="sumPage"></el-pagination>
    </div>

  </div>
</template>
<script>
import { queryPaginationIsvOperationLogs } from "@/api/api";
import elementUIFun from "@/components/js/elementUIFun.js";
import servicePub from "@/api/servicePub";
import orderType from "@/components/rule/orderType";
import seachFrom from "@/components/seachFrom/operate";
export default {
  components: {
    seachFrom
  },
  data() {
    return {
      deltedTicketAlert: false, //
      deltedTicketDetai: {
        detail: ""
      },
      goBackMoneyPop: [], //选择退款的用户
      goBackMoneyAlert: false,
      orderType: orderType,
      tableLoading: false,
      editDetailAlert: false, //
      editDetai: {}, //编辑
      pageSize: 10, //每页多少条
      thisPage: 1, //当前页
      sumPage: 0, //一共有多少条
      tableData: [], //表格数据
      multipleSelection: [], // 选择结果
      zuofei: [] //可以作废的数据
    };
  },
  computed: {
    thisActivityId() {
      return this.$store.state.activityId.thisActivityId;
    },
    fieldList() {
      return this.$store.state.activityId.fieldList;
    }
  },
  watch: {
    thisActivityId(a, b) {
      this.initFun();
    }
  },
  mounted() {
    this.initFun();
  },
  methods: {
    async initFun() {
      this.getTableData();
    },
    // 得到表格数据
    async getTableData() {
      this.tableLoading = true;
      const { data = [] } = await queryPaginationIsvOperationLogs({
        ...(this.$refs.seachFrom.saveSearchFrom || {}),
        activityId: this.$store.state.activityId.thisActivityId,
        start: (this.thisPage - 1) * this.pageSize,
        size: this.pageSize,
        isPay: "pay"
      });
      this.tableLoading = false;
      this.tableData = data.result.isvOperationLogs || [];
      this.sumPage = data.result.count || 0;
      console.log(this.tableData);
      //   console.log(data);
    },
    // 展开某行时候
    async expandChange(expandedRows) {
      if (expandedRows.children && expandedRows.children.length) {
        return;
      }
      const { data = [] } = await servicePub.queryTicketByOrderId({
        activityId: this.$store.state.activityId.thisActivityId,
        orderId: expandedRows.orderId
      });
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].orderId == expandedRows.orderId) {
          this.$set(this.tableData[i], "children", data || []);
        }
      }
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.thisPage = val;
      this.getTableData();
    },
    // 编辑详情
    editDetail(propsRow) {
      this.editDetai = JSON.parse(JSON.stringify(propsRow));
      this.editDetailAlert = true;
    },

    // 选项发生改变的时候
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.multipleSelection.map((item, i) => {
        if (item.source == 3) {
          this.zuofei.push(item);
        }
      });
    },
    //退款
    goBackMoney() {
      if (this.multipleSelection.length == 0) {
        this.$message({
          message: "请至少选择一项！",
          type: "warning"
        });
        return;
      }
      this.goBackMoneyPop = [];
      this.multipleSelection.map(item => {
        if (item.source == 4) {
          // 购票的才可以退款
          this.goBackMoneyPop.push(item);
        }
      });
      if (this.goBackMoneyPop.length == 0) {
        this.$message({
          message: "请至少选择一项下单购买的用户！",
          type: "warning"
        });
        return;
      }
      console.log(this.goBackMoneyPop);
      this.goBackMoneyAlert = true;
    },
    async returnTicketOrderByBillcode() {
      if (!this.editDetai.detail) {
        this.$message({
          message: "请输入备注内容",
          type: "warning"
        });
        return;
      }
      const confirm = await elementUIFun.$confirm(
        `此操作将进行退款, 是否继续?`
      );
      if (!confirm) {
        return;
      }
      let orderCode = [];
      this.goBackMoneyPop.map(item => {
        orderCode.push(item.orderCode);
      });
      console.log(orderCode);
      let isAllSuccessNum = 0;
      for (let i = 0; i < orderCode.length; i++) {
        const {
          success = false
        } = await servicePub.returnTicketOrderByBillcode({
          activityId: this.$store.state.activityId.thisActivityId,
          orderCode: orderCode[i],
          delTicketRemark: this.editDetai.detail
        });
        if (success) {
          isAllSuccessNum++;
        }
      }
      if (isAllSuccessNum == 0) {
        return;
      } else if (isAllSuccessNum == orderCode.length) {
        this.$message({
          message: "退款成功！",
          type: "success"
        });
      } else {
        this.$message({
          message: "部分退款成功！",
          type: "success"
        });
      }
      this.goBackMoneyAlert = false;
      this.getTableData();
    },
    // 买票用户才可以退
    // 作废门票
    async delTicketByOrderId() {
      if (this.zuofei.length == 0) {
        this.$message({
          message: "请至少选择一项可作废的门票！",
          type: "warning"
        });
        return;
      }
      this.deltedTicketAlert = true;
    }
  }
};
</script>


